import api from "../../config/api";
import { shareOptions } from "../../utils/access";
import request from "../../utils/request";
import { jump, getStorage, setStorage } from "../../utils/utils";
const app = getApp()

// pages/medalWall/medalWall.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 用户id
    id: null,
    // 勋章列表
    list: [],
    // 是否是自己
    isMine: true,
    // 是否显示分享朋友圈提示
    showTip: false,
    // 提示箭头位置
    arrowRight: 0,
    // 圈子列表
    circleList: [],
    // 显示圈子列表
    showCircleList: false,
    // 显示圈子loading
    showCircleLoading: false,
    // 分享图片
    shareImage: 'https://image-classify.daieco.com/daidai-recycle/rhdRk3VDMx8o6b5400b19458ffcd1512a3a9ee8c88c2.png',
    title: '勋章墙'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const capsule = wx.getMenuButtonBoundingClientRect()
    const systemInfo = wx.getSystemInfoSync()
    const isMine = getStorage('userId') === +options?.userId

    if (options?.inviteData) {
      setStorage('inviteData', options?.inviteData)
      console.log('邀请人数据：', options.inviteData)
    }
    if (options?.platform) {
      setStorage('platform', options?.platform)
      console.log('平台推广码数据：', options.platform)
    }

    this.setData({
      id: options?.userId,
      isMine,
      arrowRight: systemInfo.screenWidth - capsule.right + capsule.width * 3 / 4 - 5,
      title: isMine ? '我的勋章墙' : 'TA的勋章墙',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.modal = this.selectComponent('#modal')
    this.shareSheet = this.selectComponent('#shareSheet')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMedal()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getMedal()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 获取勋章墙列表数据
  async getMedal() {
    const { id } = this.data
    const res = await request.get(api.medalWall, { userId: id })
    this.setData({
      list: [...res?.data]
    })
  },
  // 显示圈子列表
  async showCircle() {
    if (this.data.showCircleLoading) {
      return
    }
    this.setData({
      showCircleLoading: true
    })
    const res = await request.get(api.getCircleListAdded)
    if (res?.httpStatus === 200) {
      // 是否有加入的圈子
      if (res?.data?.length) {
        this.setData({
          showCircle: true,
          circleList: [...res?.data]
        })
      } else {
        this.modal.show()
      }
    }
    this.setData({
      showCircleLoading: false,
    })
  },
  // 分享到圈子
  async circleShare(e) {
    const { shareImage, circleList } = this.data
    const dataset = e?.currentTarget?.dataset
    const res = await request.post(api.addDynamic,{
      circleId: dataset?.id,
      shareType: 1,
      content: '这个是我在可见分类的勋章墙，快来围观。',
      picUrls: [
        shareImage
      ]
    })
    if (res?.httpStatus === 200) {
      if (circleList[dataset?.index]) {
        circleList[dataset?.index].shared = true
        this.setData({
          circleList
        })
      }
      setTimeout(() => {
        wx.showToast({
          title: '分享成功！',
          icon:'none'
        })
      })
    }
  },
  // 显示分享弹窗
  showShareSheet() {
    this.shareSheet.show()
  },
  // 隐藏分享弹窗
  hideShareSheet() {
    this.setData({
      showCircle: false,
    })
  },
  // 分享到朋友圈提示
  showTimelineTip() {
    this.setData({
      showTip: true
    })
  },
  // 关闭提示
  closeTip() {
    this.setData({
      showTip: false
    })
  },

  // 用户点击右上角分享
  onShareAppMessage (e) {
    const { isMine, shareImage } = this.data
    if (isMine) {
      const userInfo = getStorage('userInfo')
      const userId = getStorage('userId') || ''
      return shareOptions('default', e, (userInfo?.nickName || '')  + '的勋章墙', `/pages/medalWall/medalWall?userId=${userId}&inviteData=${userId}`, shareImage)
    } else {
      return shareOptions('default',e)
    }
  },
  // 跳转圈子页面
  jumpCircle() {
    this.modal.hide()
    jump('circle', { id: 1 })
  },
  // 分享到朋友圈
  onShareTimeline (e) {
    const { isMine, shareImage } = this.data
    e.shareType = 2
    if (isMine) {
      const userInfo = getStorage('userInfo')
      const userId = getStorage('userId') || ''
      return shareOptions('default', e, (userInfo?.nickName || '')  + '的勋章墙', `userId=${userId}&inviteData=${userId}`, shareImage)
    } else {
      return shareOptions('default', e)
    }
  }
})